/*
 * Aboboo Android
 * Copyright (C) 2012 www.aboboo.com
 * 
 */

package com.aboboo.android.player;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

/**
 * 
 * @author fangpeng
 * 
 * @version 1.0 2013-01-31
 * 
 */
public class LyricPlayerService extends MediaPlayerService implements
		LyricAdapter.LyricListener {
	public class LocalBinder extends Binder {
		public LyricPlayerService getService() {
			return LyricPlayerService.this;
		}
	}

	private final IBinder mBinder = new LocalBinder();

	@Override
	IBinder getBinder() {
		return mBinder;
	}

	// 定义接受播放器状态变化的接口
	public interface LyricPlayerListener {
		// 播放位置发生变化
		public void onPositionChanged(long position);

		// 播放器状态发生变化
		public void onStateChanged();

		// 当前歌词变化
		public void onLyricChanged(int lyric_index);

		// 歌词文件解析完了
		public void onLyricLoaded();
	}

	private LyricPlayerListener mLyricPlayerListener = null;

	void setLyricPlayerListener(LyricPlayerListener listener) {
		mLyricPlayerListener = listener;
		if (mLyricPlayerListener != null) {
			// 新设定Listener直接调用onLyricLoad方法，给以个准备表是内容的机会。
			mLyricPlayerListener.onLyricLoaded();
			int curLyric = mLyricAdapter.getCurrentLyric();
			if (curLyric >= 0 && curLyric < mLyricAdapter.getLyricCount()) {
				// 如果已经处于播放中状态，将现在歌词通知给Listener
				mLyricPlayerListener.onLyricChanged(curLyric);
			}
		}
	}

	private LyricAdapter mLyricAdapter = new LyricAdapter();

	private SafetyTimer mLyricTimer = new SafetyTimer(500,
			new SafetyTimer.OnTimeListener() {
				public void OnTimer() {
					if (mMediaPlayer != null) {
						int position = mMediaPlayer.getCurrentPosition();
						if (mLyricPlayerListener != null) {
							mLyricPlayerListener.onPositionChanged(position);
						}
						mLyricAdapter.notifyTime(position);
					}
				}
			});

	@Override
	public void onCreate() {
		super.onCreate();
		mLyricAdapter.setListener(this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mLyricAdapter = null;
	}

	public void setMediaInfoProvider(
			MediaPlayerService.MediaInfoProvider provider) {
		super.setMediaInfoProvider(provider);
		loadLyric();
	}

	@Override
	public void start() {
		if (isStop()) {
			// 停止状态开始，需要重新启动Timer
			super.start();
			mLyricTimer.startTimer();
		} else {
			// 暂停状态开始，不需要重启Timer
			super.start();
		}
		if (mLyricPlayerListener != null) {
			mLyricPlayerListener.onStateChanged();
		}
	}

	@Override
	public void stop() {
		mLyricTimer.stopTimer();
		super.stop();
		if (mLyricPlayerListener != null) {
			mLyricPlayerListener.onStateChanged();
		}
	}

	@Override
	public void pause() {
		super.pause();
		if (mLyricPlayerListener != null) {
			mLyricPlayerListener.onStateChanged();
		}
	}

	@Override
	public void playNext() {
		seekToNextLyric();
	}

	@Override
	public void playPrev() {
		seekToPrevLyric();
	}

	public void loadLyric() {
		String url = getDataSource();
		// 和歌曲在同一目录下，相同文件名，扩展名为lrc的文件
		String strLyricFileUrl = url.substring(0, url.lastIndexOf(".") + 1)
				+ "lrc";
		mLyricAdapter.LoadLyric(strLyricFileUrl);
	}

	public int getLyricCount() {
		return mLyricAdapter.getLyricCount();
	}

	public String getLyric(int index) {
		return mLyricAdapter.getLyric(index);
	}

	public long seekToLyric(int index) {
		long position = mLyricAdapter.getLyricTime(index);
		if (position != -1) {
			return seek(position);
		} else {
			return 0;
		}
	}

	public void seekToPrevLyric() {
		int curLyric = mLyricAdapter.getCurrentLyric();
		if (curLyric > 0) {
			seekToLyric(curLyric - 1);
		}
	}

	public void seekToNextLyric() {
		int curLyric = mLyricAdapter.getCurrentLyric();
		if (curLyric < mLyricAdapter.getLyricCount() - 1) {
			seekToLyric(curLyric + 1);
		}
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		if (mLyricTimer.isRunging()) {
			mLyricTimer.stopTimer();
		}
		if (mLyricPlayerListener != null) {
			mLyricPlayerListener.onStateChanged();
		}
		super.onCompletion(mp);
	}

	@Override
	public void onLyricChanged(int lyric_index) {
		if (mLyricPlayerListener != null) {
			mLyricPlayerListener.onLyricChanged(lyric_index);
		}
	}

	@Override
	public void onLyricLoaded() {
		if (mLyricPlayerListener != null) {
			mLyricPlayerListener.onLyricLoaded();
			mLyricPlayerListener.onStateChanged();
		}
	}
}
